package engine;

import java.io.PrintWriter;
import java.util.ArrayList;

public class Logger {

	private static ArrayList<LogListener> listeners;
	
	static {
		listeners = new ArrayList<LogListener>();
	}
	public static void log(String msg) {
		
//		System.out.println(msg);
		
		for (int i=0; i<listeners.size(); i++) {
			try {
				LogListener ll = listeners.get(i);
				ll.handleMessage(msg);
				
			} catch (Exception e) {
				listeners.remove(i);
				i--;
			}
		}
	}
	
	public static void printStackTrace(Exception e) {
		
		StackTraceElement[] stes = e.getStackTrace();
		for (int i = stes.length-1; i >= 0 ; i--) {
			log(stes[i].toString());
		}
		log(e.toString());
		
		
	}
	
	public static void addLogListener(LogListener ll) {
		if (!listeners.contains(ll))
			listeners.add(ll);
	}
	public static void removeListener(LogListener ll) {
		listeners.remove(ll);
	}
	
	public interface LogListener {
		public void handleMessage(String msg);
	}
}
